package com.tss.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tss.pojo.Optionen;
import com.tss.pojo.Question;
import com.tss.pojo.QuestionBean;
import com.tss.pojo.Result;
import com.tss.service.OptionenService;
import com.tss.service.QuestionService;
import com.tss.vo.Dictionary;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("question")
public class QuestionController {
	
	@Autowired
	private QuestionService questionService;
	
	@Autowired
	private OptionenService optionenService;
	
	/**
	 * 新增问题
	 * 及其选项
	 * @param questionBean
	 * @return
	 */
	@ResponseBody
	@RequestMapping("add")
	public String add(QuestionBean questionBean){
		Result result = new Result();
		if(questionBean==null||questionBean.getQuestionnaireId()==null||questionBean.getContent()==null){
			return result.setErrorMessage("参数错误！").toString();
		}
		Question question = new Question();
		question.setQuestionnaireId(questionBean.getQuestionnaireId());
		question.setContent(questionBean.getContent());
		question.setType(questionBean.getType());
		result=questionService.createQuestion(question);
		if(question.getId()==null){
			return result.toString();
		}
		if(questionBean.getType()==Dictionary.QUESTION_TYPE_SINGLECHOICE||questionBean.getType()==Dictionary.QUESTION_TYPE_MULTIPLECHOICE){
			Optionen optionen = new Optionen();
			optionen.setQuestionId(question.getId());
			optionen.setContent(questionBean.getA());
			optionen.setSigns(Dictionary.Optionen_SIGNS_A);
			optionenService.addOptionen(optionen);
			optionen.setContent(questionBean.getB());
			optionen.setSigns(Dictionary.Optionen_SIGNS_B);
			optionenService.addOptionen(optionen);
			optionen.setContent(questionBean.getC());
			optionen.setSigns(Dictionary.Optionen_SIGNS_C);
			optionenService.addOptionen(optionen);
			optionen.setContent(questionBean.getD());
			optionen.setSigns(Dictionary.Optionen_SIGNS_D);
			optionenService.addOptionen(optionen);
		}
		
		return result.toString();
	}

	/**
	 * 删除问题
	 * 及其选项
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete")
	public String delete(Long id){
		Result result = questionService.deleteQuestion(id);
		if(result.getResult().containsKey("type")&&(Long)(result.getResult().get("type"))!=3L){
			Optionen optionen = new Optionen();
			optionen.setQuestionId(id);
			optionenService.deleteOptionen(optionen);
		}
		return result.toString();
	}
	
	/**
	 * 问题列表
	 * @param questionnaireId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("list")
	public String list(Long questionnaireId){
		List<Question> list = questionService.listQuestion(questionnaireId);
		if(list==null||list.isEmpty()){
			return null;
		}
		List<QuestionBean> questionBeans = new ArrayList<QuestionBean>();
		for(Question question:list){
			QuestionBean questionBean = new QuestionBean();
			questionBean.setContent(question.getContent());
			questionBean.setId(question.getId());;
			questionBean.setType(question.getType());
			questionBean.setQuestionnaireId(questionnaireId);
			if(question.getType()==Dictionary.QUESTION_TYPE_SINGLECHOICE||question.getType()==Dictionary.QUESTION_TYPE_MULTIPLECHOICE){
				List<Optionen> optionens = optionenService.listOptionen(question.getId());
				for(Optionen optionen:optionens){
					if(optionen.getSigns()==Dictionary.Optionen_SIGNS_A){
						questionBean.setA(optionen.getContent());
					}else
					if(optionen.getSigns()==Dictionary.Optionen_SIGNS_B){
						questionBean.setB(optionen.getContent());
					}else
					if(optionen.getSigns()==Dictionary.Optionen_SIGNS_C){
						questionBean.setC(optionen.getContent());
					}else
					if(optionen.getSigns()==Dictionary.Optionen_SIGNS_D){
						questionBean.setD(optionen.getContent());
					}
				}
			}
			questionBeans.add(questionBean);
		}
		return JSONArray.fromObject(questionBeans).toString();
	}
	
	
	@ResponseBody
	@RequestMapping("update")
	public String update(QuestionBean questionBean){
		Result result = new Result();
		if(questionBean==null||questionBean.getId()==null){
			return result.setErrorMessage("参数错误！").toString();
		}
		Question question = new Question();
		question.setId(questionBean.getId());
		question.setContent(questionBean.getContent());
		result=questionService.updateQuestion(question);
		if(result.getStatus()==Result.STATUS_CODE_SERVER_ERRORS){
			return result.toString();
		}
		if(question.getType()==Dictionary.QUESTION_TYPE_SINGLECHOICE||question.getType()==Dictionary.QUESTION_TYPE_MULTIPLECHOICE){
			Optionen optionen = new Optionen();
			optionen.setQuestionId(question.getId());
			optionen.setContent(questionBean.getA());
			optionen.setSigns(Dictionary.Optionen_SIGNS_A);
			optionenService.updateOptionen(optionen);
			optionen.setContent(questionBean.getB());
			optionen.setSigns(Dictionary.Optionen_SIGNS_B);
			optionenService.updateOptionen(optionen);
			optionen.setContent(questionBean.getC());
			optionen.setSigns(Dictionary.Optionen_SIGNS_C);
			optionenService.updateOptionen(optionen);
			optionen.setContent(questionBean.getD());
			optionen.setSigns(Dictionary.Optionen_SIGNS_D);
			optionenService.updateOptionen(optionen);
		}
		return result.toString();
	}
	

}
